package com.grablist.feihu.grablist.ui.fragment.presenter;

import android.content.Intent;

import com.grablist.feihu.grablist.base.BaseBean;
import com.grablist.feihu.grablist.base.BaseBiz;
import com.grablist.feihu.grablist.base.BaseFragment;
import com.grablist.feihu.grablist.bean.ClueInfo;
import com.grablist.feihu.grablist.bean.UserInfo;
import com.grablist.feihu.grablist.comm.Constants;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.ui.actviity.login.CheckAct;
import com.grablist.feihu.grablist.ui.actviity.user.ApplyCustomAct;
import com.grablist.feihu.grablist.ui.actviity.user.CertificationAct;
import com.grablist.feihu.grablist.ui.actviity.user.ExportDataAct;
import com.grablist.feihu.grablist.ui.fragment.biz.CustomClueBiz;
import com.grablist.feihu.grablist.ui.fragment.contract.CustomClueContract;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class CustomCluePresent implements CustomClueContract.Presenter {

    private CustomClueContract.View mView;
    private CustomClueBiz mBiz;

    private int index = 1;
    private List<ClueInfo> clueInfoList = new ArrayList<>();

    @Inject
    public CustomCluePresent(CustomClueContract.View view) {
        this.mView = view;
    }

    @Override
    public void setBiz(BaseBiz biz) {
        mBiz = (CustomClueBiz) biz;
    }

    @Override
    public void getBatchInfoList() {
        mView.loading(true);
        mBiz.getBatchInfoByUserId(MyApplication.userInfo.getUserCode(), Constants.PAGESIZE, index, new BaseBiz.Callback<BaseBean<List<ClueInfo>>>() {
            @Override
            public void onSuccess(BaseBean<List<ClueInfo>> o) {
                if (index == 1 && o.getList().size() == 0) {
                    mView.setNoDate(true);
                } else {
                    mView.setNoDate(false);
                }
                clueInfoList.addAll(o.getList());
                mView.updateList(clueInfoList);
                if (index > 1) {
                    mView.loadMoreComplete();
                } else {
                    mView.refreshComplete();
                }
                mView.loading(false);
                mView.hasLoadMore(index * Constants.PAGESIZE >= o.getTotal());
                index++;
            }

            @Override
            public void onFailure(BaseBean<List<ClueInfo>> listBaseBean) {
                if (index > 1) {
                    mView.loadMoreComplete();
                } else {
                    mView.refreshComplete();
                }
                mView.loading(false);
            }
        });
    }

    @Override
    public void refresh() {
        index = 1;
        clueInfoList.clear();
        mView.updateList(clueInfoList);
        mView.hasLoadMore(false);
    }

    @Override
    public void getUserInfo() {
        mBiz.getUserInfo(MyApplication.userInfo.getUserCode(), new BaseBiz.Callback<List<UserInfo>>() {

            @Override
            public void onSuccess(List<UserInfo> o) {
                mView.updateUI(o.get(0));
            }

            @Override
            public void onFailure(List<UserInfo> o) {

            }
        });
    }

    @Override
    public void toApplyCustom() {
        mView.startAct(ApplyCustomAct.class, null);
    }

    @Override
    public void toApplyMore() {
        mView.startAct(CheckAct.class, null);
    }

    @Override
    public void toExportData() {
        Intent intent = new Intent(((BaseFragment) mView).getActivity(), ExportDataAct.class);
        intent.putExtra("isCustom", true);
        mView.startAct(intent);
    }
}
